package com.example.tests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;
import com.example.fw.Contacts;

public class ContactDataGenerator {

	protected static ApplicationManager app;
	
	static Random rnd = new Random();
	
	@DataProvider(name = "randomContacts")
	public static Iterator<Object[]> generateRandomContact() {
		List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactList(5);
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObject contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}

	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() {
		List<Object[]> list = new ArrayList<Object[]>();
		return list.iterator();
	}
	
	public static void main(String[] args) {
		if (args.length < 2) {
			System.out.println("Specify two parameters: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		new ContactDataGenerator().generateDataToFile(file, count);
	}

	private void generateDataToFile(String file, int count) {
		List<ContactObject> contacts = generateRandomContactList(count);		
	}
	
	private List<ContactObject> generateRandomContactList(int count) {
		List<ContactObject> list = new ArrayList<ContactObject>();
		for (int i = 0; i < count; i++) {
			ContactObject contact = generateRandomContactData();
			list.add(contact);
		}
		return list;
	}

	private static ContactObject generateRandomContactData() {
		ContactObject contact = new ContactObject()
			.setFirstName("FirstName"+rnd.nextInt())
			.setLastName("LastName"+rnd.nextInt())
			.setMainAddress("MainAddress"+rnd.nextInt())
			.setTelephoneHome("TelephoneHome"+rnd.nextInt())
			.setTelephoneMobile("TelephoneMobile"+rnd.nextInt())
			.setTelephoneWork("TelephoneWork"+rnd.nextInt())
			.setMainEmail("MainEmail@"+rnd.nextInt())
			.setAlternativeEmail("AlternativeEmail@"+rnd.nextInt())
			.setBirthDay("1")
			.setBirthMonth("December")
			.setBirthYear("2012")
			.setChooseGroup("[none]")
			.setAlternativeAddress("AlternativeAddress"+rnd.nextInt())
			.setAlternativeTelephoneHome("AlternativeTelephoneHome"+rnd.nextInt());
		return contact;
	}

	
	@DataProvider(name = "existingAndRandomContacts")
	 public static Iterator<Object[]> generatePairOfExistingAndRandomContact() {
		 List<Object[]> list = new ArrayList<Object[]>();
		 ContactObject existingContact = getExistingContact();
		 ContactObject newContact = generateRandomContactData();
		 list.add(new Object[]{existingContact, newContact});
		 return list.iterator();
	 }
	 
	@DataProvider(name = "getSomeExistingContact")
	public static Iterator<Object[]> getSomeExistingContact() {
		ContactObject someContact = getExistingContact();
		// формируем список наборов тестовых данных для тестового метода
		List<Object[]> result = new ArrayList<Object[]>();
		// метод принимает один параметр ContactObject
		// поэтому каждый тестовый набор -- это массив из одного объекта типа ContactObject
		result.add(new Object[]{someContact});
		// возвращаем итератор набора тестовых данных
		return result.iterator();
	}

	private static ContactObject getExistingContact() {
		// получаем множество контактов
		Contacts contacts = app.getContactHelper().getContacts();
		// выбираем случайный контакт (какой попадётся)
		ContactObject someContact = contacts.getStoredContacts().iterator().next();
		return someContact;
	}
	
}
